<?php

namespace App\Http\Middleware;

use Closure;
use Exception;
use farm\utils\ApiUtils;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\Response;

class RequestMini
{
    /**
     * 处理传入请求。
     *
     * @param Closure(Request): (Response) $next
     * @throws Exception
     */
    public function handle(Request $request, Closure $next):\Illuminate\Http\Response
    {
        ApiUtils::setParams(ApiUtils::input($request));
        ApiUtils::setQueryParams($request->all());

        $token = $request->header('Authorization');
        if ($token) {
            $info = DB::table('user_tokens')->where(['token' => $token])->first();
            //before
            ApiUtils::setUserinfo($info);
            ApiUtils::setToken($token);
        }

        return $next($request);
    }

    /**
     * 在响应发送到浏览器后处理任务。
     */
    public function terminate(Request $request, Response $response): void
    {

    }
}
